﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Dynamic;
using System.Transactions;
using PagedList;
using Sovigaz.DataProvider;
using Utilities;

namespace Sovigaz.Permission.Repositories
{
    public class PartnerRepository : GenericRepository
    {
        public PartnerRepository()
            : base("PartnerID")
        {
        }

        public ICollection<PartnerComplex> Partners { get; set; }

        public void Delete(int id)
        {
            var item = GetByID(id);
            var orderrepository = new OrderRepository();
            if (item != null && item.PartnerID>1)
            {
                if (_context.Orders.Count(o => o.PartnerID == item.PartnerID) > 0) return;
                if (_context.Products.Count(p => p.OwnerID == item.PartnerID) > 0) return;
                _context.Partners.Remove(item);
            }
            _context.SaveChanges();
        }

        public void Delete(string[] idArr)
        {
            foreach (var id in idArr)
                Delete(ConvertUtility.IntParse(id));

            _context.SaveChanges();
        }

        public int Update(Partner partner)
        {
            var updatePartner = new Partner();

            using (var scope = new TransactionScope())
            {
                if (partner.PartnerID > 0)
                {
                    updatePartner = GetByID(partner.PartnerID);
                    if (!Utility.ArrayEqual(updatePartner.Tstamp, partner.Tstamp))
                        throw ExceptionHelper.CreateHttpResponseException("Dữ liệu đã bị cập nhật bởi người sử dụng khác.");
                }
                updatePartner.PartnerID = partner.PartnerID;
                updatePartner.LocationID = partner.LocationID;
                updatePartner.PartnerCode = partner.PartnerCode;
                updatePartner.PartnerName = partner.PartnerName;
                updatePartner.ContactName1 = partner.ContactName1;
                updatePartner.ContactName2 = partner.ContactName2;
                updatePartner.Address1 = partner.Address1;
                updatePartner.Address2 = partner.Address2;
                updatePartner.Phone1 = partner.Phone1;
                updatePartner.Phone2 = partner.Phone2;
                updatePartner.Representer = partner.Representer;
                updatePartner.Role = partner.Role;
                updatePartner.Description = partner.Description;

                if (updatePartner.PartnerID == 0)
                    _context.Partners.Add(updatePartner);

                _context.SaveChanges();

                scope.Complete();
            }

            _context.Entry(updatePartner).Reload();

            return updatePartner.PartnerID;
        }

        public Partner GetByID(int id)
        {
            return _context.Partners.FirstOrDefault(p => p.PartnerID == id && !p.IsDeleted);
        }

        public PartnerRepository GetList(string whereClause, string orderBy, string orderDirection, int pageNumber)
        {
            var query = _context.Partners.Where(whereClause).Select(p => new PartnerComplex()
            {
                PartnerID = p.PartnerID,
                LocationID = p.LocationID,
                LocationName = _context.Locations.Count(lo => lo.LocationID == p.LocationID) > 0 ? _context.Locations.FirstOrDefault(l => l.LocationID == p.LocationID).LocationName : string.Empty,
                PartnerCode = p.PartnerCode,
                PartnerName = p.PartnerName,
                Representer = p.Representer,
                Role = p.Role,
                Address1 = p.Address1,
                Address2 = p.Address2,
                ContactName1 = p.ContactName1,
                ContactName2 = p.ContactName2,
                Phone1 = p.Phone1,
                Phone2 = p.Phone2,
                Description = p.Description,
                IsDeleted = p.IsDeleted
            })
            .Where(whereClause)
            .OrderBy(String.Format("{0} {1}", orderBy.Trim(), orderDirection.Trim()))
            .ToPagedList(pageNumber, DefaultPageSize);

            TotalItemCount = query.TotalItemCount;
            TotalItemOnPage = query.Count;
            PageCount = query.PageCount;
            PageNumber = query.PageNumber;
            Partners = query.ToList();

            return this;
        }

        public List<PartnerComplex> GetListPartnerComplex()
        {
            var query = _context.Partners.Where(p => !p.IsDeleted).Select(p => new PartnerComplex()
            {
                PartnerID = p.PartnerID,
                LocationID = p.LocationID,
                LocationName = _context.Locations.Count(lo => lo.LocationID == p.LocationID) > 0 ? _context.Locations.FirstOrDefault(l => l.LocationID == p.LocationID).LocationName : string.Empty,
                PartnerCode = p.PartnerCode,
                PartnerName = p.PartnerName,
                Representer = p.Representer,
                Role = p.Role,
                Address1 = p.Address1,
                Address2 = p.Address2,
                ContactName1 = p.ContactName1,
                ContactName2 = p.ContactName2,
                Phone1 = p.Phone1,
                Phone2 = p.Phone2,
                Description = p.Description
            });
            return query.ToList();
        }

        public List<Partner> GetListPartner()
        {
            return _context.Partners.Where(p => !p.IsDeleted).ToList();
        }
    }
}
